import indexTpl from '../views/index.art';
import pageHeader from '../components/pageheader';
import {
  authModel
} from '../models/auth';
import img from '../assets/user2-160x160.jpg';
import page from '../databus/page';


//首页
const index = (router) => {
  return async (req, res, next) => {
    let result = await authModel();
    if (result.ret) {
      const html = indexTpl({
        subRouter: res.subRoute(),
        img: img
      })
      //渲染首页
      next(html);

      // res.render();
      //打补丁 window resize 让页面撑满整个屏幕
      $(window, '.wrapper').resize();

      //加载页面导航
      pageHeader();

      //左侧导航栏高亮显示
      const $as = $('#sidebar-menu li:not(:first-child) a');
      let hash = location.hash;
      $as
        .filter(`[href="${hash}"]`)
        .parent()
        .addClass('active')
        .siblings()
        .removeClass('active');

      //是否重置页码
      if (hash !== page.currentRoute) {
        page.reset()
      }

      //当前url保存
      page.setCurrentRoute(hash);

      //登出事件绑定
      $('#users-signout').on('click', (e) => {
        e.preventDefault();
        localStorage.setItem('lg-token', '');
        location.reload();
      });

      //socket
      var socket = io.connect('http://localhost:3000');

      socket.on('message', function (msg) {
        let num = ~~$('#icon-email').text();
        $('#icon-email').text(++num);
      })

    } else {
      router.go('/signin');
    }
  };
};

export default index;